<template>
	<el-dialog
		:title="operate === 'add' ? '添加拼团' : '编辑拼团'"
		:visible.sync="visible"
		width="950px"
	>
		<el-form ref="form" :model="form" :rules="rules" label-width="130px">
			<div class="form-group">
				<el-form-item label="商品ID：" style="width: 80%" prop="goodsId">
					<el-input
						v-model="form.goodsId"
						size="small"
						clearable
						:disabled="operate === 'edit'"
						placeholder="请填写商品ID"
					></el-input>
				</el-form-item>
				<el-button type="primary" class="search-btn" size="small" @click="onSearch"
					>检索</el-button
				>
			</div>
			<el-form-item label="商品信息：" v-if="goodsInfo.id" style="width: 80%">
				<div class="goods-info">
					<p>{{ goodsInfo.title }}</p>
					<p>
						售价：¥{{ goodsInfo.endprice }} 成本价：¥{{ goodsInfo.costprice }} 原价：¥{{
							goodsInfo.price
						}}
						利润：¥{{
							parseFloat(goodsInfo.endprice) - parseFloat(goodsInfo.costprice)
						}}
						库存：{{ goodsInfo.stock }}
					</p>
				</div>
			</el-form-item>
			<div class="form-group">
				<el-form-item label="开团人数：" style="width: 50%" prop="groupUserNumber">
					<el-input
						v-model="form.groupUserNumber"
						clearable
						size="small"
						placeholder="请填写开团人数"
						:disabled="form.newPerson === 1"
					></el-input>
				</el-form-item>
				<el-form-item label="开团机器人数：" style="width: 50%">
					<el-input
						v-model="form.groupRobotUserNumber"
						clearable
						size="small"
						placeholder="请填写开团机器人数"
						:disabled="form.newPerson === 1"
					></el-input>
				</el-form-item>
			</div>
			<div class="form-group">
				<el-form-item label="拼团时间：" style="width: 50%" prop="time">
					<el-date-picker
						v-model="form.time"
						type="datetimerange"
						size="small"
						style="width: 100%"
						range-separator="-"
						clearable
						value-format="yyyy-MM-dd HH:mm:ss"
						start-placeholder="开始日期"
						end-placeholder="结束日期"
					>
					</el-date-picker>
				</el-form-item>
				<el-form-item label="拼团让利%：" style="width: 40%" prop="profitRate">
					<el-input
						v-model="form.profitRate"
						size="small"
						clearable
						placeholder="请填写拼团让利%"
						@change="onProfitChange"
					></el-input>
				</el-form-item>
				<p
					style="height: 30px;margin-top: 12px;margin-left: 10px;color: red"
					v-if="goodsInfo.id && form.profitRate"
				>
					¥{{
						(
							((parseFloat(goodsInfo.endprice) - parseFloat(goodsInfo.costprice)) *
								parseFloat(form.profitRate)) /
							100
						).toFixed(2)
					}}
				</p>
			</div>
			<el-form-item label="排序：" style="width: 50%">
				<el-input-number
					v-model="form.level"
					style="width: 100%"
					:precision="0"
					size="small"
					:step="1"
					:min="1"
				></el-input-number>
			</el-form-item>
			<el-form-item label="是否新人专享：" style="width: 50%" prop="newPerson">
				<el-radio-group v-model="form.newPerson" @change="onRadioChange">
					<el-radio :label="0">否</el-radio>
					<el-radio :label="1">是</el-radio>
				</el-radio-group>
			</el-form-item>
		</el-form>
		<div class="dialog-footer">
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" @click="onSubmit">确 定</el-button>
		</div>
	</el-dialog>
</template>

<script>
export default {
	name: 'AddDig',
	data() {
		return {
			visible: false,
			goodsInfo: {},
			operate: 'add',
			isShowgoodsInfo: false,
			form: {
				goodsId: '',
				groupUserNumber: '',
				groupRobotUserNumber: '',
				time: [],
				profitRate: '',
				level: 1,
				newPerson: 0
			},
			rules: {
				goodsId: [{ required: true, message: '商品ID不能为空', trigger: 'blur' }],
				groupUserNumber: [{ required: true, message: '开团人数不能为空', trigger: 'blur' }],
				groupRobotUserNumber: [
					{ required: true, message: '开团机器人数不能为空', trigger: 'blur' }
				],
				time: [{ required: true, message: '请选择拼团开始时间', trigger: 'blur' }],
				profitRate: [{ required: true, message: '拼团让利不能为空', trigger: 'blur' }],
				newPerson: [{ required: true, message: '新人专享状态不能为空', trigger: 'blur' }]
			}
		};
	},
	methods: {
		open(operate, from) {
			this.operate = operate;
			this.visible = true;
			this.goodsInfo = {};
			if (operate === 'edit') {
				this.form.goodsId = from.goodsId;
				this.form.groupUserNumber = from.groupUserNumber;
				this.form.groupRobotUserNumber = from.groupRobotUserNumber;
				this.form.time = [from.startTime, from.endTime];
				this.form.profitRate = from.profitRate;
				this.form.level = from.level;
				this.form.newPerson = from.newPerson;
				this.form.id = from.id;
			} else {
				this.form = {
					goodsId: '',
					groupUserNumber: '',
					groupRobotUserNumber: '',
					time: [],
					profitRate: '',
					level: 1,
					newPerson: 0
				};
			}
		},
		close() {
			this.visible = false;
		},
		onSearch() {
			if (!this.form.goodsId) {
				this.$message.error('商品ID不能为空');
				return false;
			}
			this.$service.goods.manage
				.info({
					id: this.form.goodsId
				})
				.then(res => {
					if (res.status === 1 && res.isDel === 0 && parseInt(res.stock) > 0) {
						this.goodsInfo = res;
					} else {
						this.$message.error('未检索到商品');
						this.goodsInfo = {};
					}
				})
				.catch(() => {
					this.$message.error('未检索到商品');
					this.goodsInfo = {};
				});
		},
		onProfitChange() {
			if (!this.goodsInfo.id) {
				this.$message.error('请先检索商品');
				this.form.profitRate = '';
				return false;
			}
		},
		onRadioChange(e) {
			if (e === 1) {
				this.form.groupUserNumber = 30;
				this.form.groupRobotUserNumber = 28;
			}
		},
		onSubmit() {
			if (!this.goodsInfo.id) {
				this.$message.error('请先检索商品');
				return false;
			}
			if (parseInt(this.goodsInfo.stock) === 0) {
				this.$message.error('库存不足');
				return false;
			}
			if (parseInt(this.form.groupUserNumber) > 50) {
				this.$message.error('开团人数不能超过50人');
				return false;
			}
			if (this.operate === 'add') {
				// 让利比例不正确
				if (
					parseFloat(this.form.profitRate) >= 0 &&
					parseFloat(this.form.profitRate) <= 100
				) {
					this.$refs['form'].validate(valid => {
						if (valid) {
							let params = {
								...this.form,
								groupRobotUserNumber: !this.form.groupRobotUserNumber
									? '0'
									: this.form.groupRobotUserNumber,
								level: !this.form.level ? 1 : this.form.level,
								startTime: this.form.time[0],
								endTime: this.form.time[1],
								groupPrice: this.goodsInfo.endprice,
								costprice: this.goodsInfo.costprice
							};
							this.$service.group.goodsCombination.add(params).then(() => {
								this.$message.success('添加成功');
								this.visible = false;
								this.$emit('refresh');
							});
						}
					});
				} else {
					this.$message.error('请输入正确的让利比例');
					return false;
				}
			} else {
				if (
					parseFloat(this.form.profitRate) >= 0 &&
					parseFloat(this.form.profitRate) <= 100
				) {
					this.$refs['form'].validate(valid => {
						if (valid) {
							let params = {
								...this.form,
								level: !this.form.level ? 1 : this.form.level,
								groupRobotUserNumber: !this.form.groupRobotUserNumber
									? '0'
									: this.form.groupRobotUserNumber,
								startTime: this.form.time[0],
								endTime: this.form.time[1],
								groupPrice: this.goodsInfo.endprice,
								costprice: this.goodsInfo.costprice
							};
							this.$service.group.goodsCombination.update(params).then(() => {
								this.$message.success('修改成功');
								this.visible = false;
								this.$emit('refresh');
							});
						}
					});
				} else {
					this.$message.error('请输入正确的让利比例');
					return false;
				}
			}
		}
	}
};
</script>

<style lang="stylus" scoped>
.form-group {
    display flex
    .search-btn {
        height 30px
        margin-top 5px
        margin-left 10px
    }
}
</style>
